package com.wsz.logistic.controller;

import com.wsz.logistic.bean.Deliverier;
import com.wsz.logistic.bean.Userinfo;
import com.wsz.logistic.common.form.LoginForm;
import com.wsz.logistic.common.form.RegisterForm;
import com.wsz.logistic.common.util.R;
import com.wsz.logistic.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserinfoService userinfoService;


    /**
     * 后台登录
     *
     * @param loginForm
     * @param session
     * @return
     */
    @PostMapping("/vip/login")
    @ResponseBody
    public R login(LoginForm loginForm, HttpSession session) {
        Userinfo userinfo = userinfoService.login(loginForm);
        if (userinfo != null) {
            if (userinfo.getStatus() == 1) {
                session.setAttribute("loginInfo", userinfo);
                Deliverier deliverier = userinfoService.getDById(userinfo.getUserinfoId());
                session.setAttribute("deliverier", deliverier);
                return R.ok(userinfo.getRole());
            } else {
                return R.error("账号异常");
            }
        } else {
            return R.error("用户名或密码不正确");
        }
    }

    @PostMapping("/vip/register")
    @ResponseBody
    public R register(RegisterForm registerForm) {
        userinfoService.register(registerForm);
        return R.ok();
    }

    @GetMapping("/nologin")
    public String noLogin() {
        return "admin/nologin";
    }

}
